<?php
namespace app\index\controller;
use think\Controller;
use think\WeixinPay;

class Company extends Controller
{


    



    /**
     * 申请
     * @return [type] [description]
     */
    public function set()
    {
        $post = input('post.');
        if(empty($post['user_id'])){
            $this->ajaxReturn(null, '参数错误', 201);
        }

        $user = db('user')->find($post['user_id']);
        if(empty($user)){
            $this->ajaxReturn(null, '无此用户', 205);
        }

        if(empty($post['company_name'])){
            $this->ajaxReturn(null, '未填写公司名称', 202);
        }
        
        if(empty($post['logo'])){
            $this->ajaxReturn(null, '未上传公司logo', 203);
        }

        if(empty($post['license'])){
            $this->ajaxReturn(null, '未上传公司营业执照', 203);
        }

        // if(empty($user['industry'])){
        //     $this->ajaxReturn(null, '未填写公司行业', 206);
        // }
        
        if(empty($post['nature'])){
            $this->ajaxReturn(null, '未选择公司性质', 203);
        }

        if(empty($post['scale'])){
            $this->ajaxReturn(null, '未选择人员规模', 203);
        }

        if(empty($post['realname'])){
            $this->ajaxReturn(null, '未填写公司负责人姓名', 203);
        }

        if(empty($post['company_mobile'])){
            $this->ajaxReturn(null, '未填写公司负责人电话', 203);
        }

        if(empty($post['company_address'])){
            $this->ajaxReturn(null, '未填写公司地址', 203);
        }

        
        $post['viewnum']    = db('config')->value('viewnum');
        $post['status']     = 1;
        $post['redbagmin']  = 10;
        $post['redbagmax']  = 100;
        $post['ctime'] 	    = time();
        $post['cdate'] 	    = date('Y-m-d H:i:s');
        $res  = db('company')->insertGetId($post);
        if($res){   
            $this->ajaxReturn(null, '申请成功，等待审核', 200);
        }else{
            $this->ajaxReturn(null, '申请失败', 204);
        }
    }


    /**
     * 设置红包
     *
     * @return void
     */
    public function setaccount()
    {
        $post = input('post.');
        if(empty($post['id'])){
            $this->ajaxReturn(null, '参数错误', 201);
        }

        $company = db('company')->find($post['id']);
        if(empty($company)){
            $this->ajaxReturn(null, '无此公司数据', 205);
        }

        if(empty($post['redbagmin']) || empty($post['redbagmax'])){
            $this->ajaxReturn(null, '未填写红包金额', 203);
        }

        $update['redbagmin']  = $post['redbagmin'];
        $update['redbagmax']  = $post['redbagmax'];
        $res  = db('company')->where('id', $post['id'])->update($update);
        if($res!==false){   
            $this->ajaxReturn(['id'=>$post['id']], '设置成功', 200);
        }else{
            $this->ajaxReturn(null, '设置失败', 204);
        }
    }


    /**
     * 福利列表 function
     *
     * @return void
     */
    public function attach()
    {
        $name = input('get.city');

        $city = [];
        if($name){
            $city = db('city')->alias('c1')->join('city c2', 'c1.id=c2.pid')->where('c1.name', $name)->select();
        }

        $list['region'] = $city;
        $list['jobtype'] = db('jobtype')->select();
        $list['welfare'] = db('welfare')->select();
        $list['nature'] = ['个体户','私企','国企'];
        $list['scale'] = ['10-99人','100-1000人','1000人以上'];
		if ($list) {
            $this->ajaxReturn($list, 'ok', 200);
        }else{
            $this->ajaxReturn(null, '数据为空', 200);
        }
    }



    /**
    **公司中心
    **/
    public function index()
    {
        $userId = input('get.user_id');
        if(empty($userId)){
            $this->ajaxReturn(null, '参数错误', 201);
        }

        $company = db('company')->where('user_id', $userId)->find();
		if ($company) {
            $this->ajaxReturn($company, 'ok', 200);
        }else{
            $this->ajaxReturn(null, '数据为空', 200);
        }
    }
    
    

    /**
     * 发布职位
     *
     * @return void
     */
    public function publish()
    {
        $post = input('post.');
        if(empty($post['user_id'])){
            $this->ajaxReturn(null, '参数错误', 201);
        }

        $user = db('user')->find($post['user_id']);
        if(empty($user)){
            $this->ajaxReturn(null, '无此用户', 205);
        }

        $company = db('company')->where('user_id', $post['user_id'])->field('id,user_id,company_name,company_mobile,logo,status')->find();
        if(empty($company)){
            $this->ajaxReturn(null, '无此公司', 205);
        }

        if($company['status']!=2){
            $this->ajaxReturn(null, '还在审核中', 205);
        }

        if(empty($post['jobtype'])){
            $this->ajaxReturn(null, '未填写职位类型', 203);
        }

        if(empty($post['title'])){
            $this->ajaxReturn(null, '未填写职位名称', 203);
        }

        if(empty($post['name'])){
            $this->ajaxReturn(null, '未填写负责人姓名', 203);
        }

        if(empty($post['mobile'])){
            $this->ajaxReturn(null, '未填写负责人电话', 203);
        }

        if(empty($post['region_id'])){
            $this->ajaxReturn(null, '未填写工作区域', 203);
        }

        if(empty($post['region_name'])){
            $this->ajaxReturn(null, '未填写工作区域', 203);
        }

        if(empty($post['longitude'])){
            $this->ajaxReturn(null, '未获取经度', 203);
        }

        if(empty($post['latitude'])){
            $this->ajaxReturn(null, '未获取纬度', 203);
        }

        if(empty($post['salary'])){
            $this->ajaxReturn(null, '未填写工作薪资', 203);
        }

        if(empty($post['rebate'])){
            $this->ajaxReturn(null, '未填写返利佣金', 203);
        }

        if(empty($post['sex'])){
            $this->ajaxReturn(null, '未填写性别', 203);
        }

        if(empty($post['agemin'])){
            $this->ajaxReturn(null, '未填写年龄范围', 203);
        }

        if(empty($post['agemax'])){
            $this->ajaxReturn(null, '未填写年龄范围', 203);
        }

        if(empty($post['welfare'])){
            $this->ajaxReturn(null, '未填写公司福利', 203);
        }

        $id = !empty($post['id'])?$post['id']:0;
        unset($post['user_id']);
        if($id){
            $data = $post;
            $res  = db('position')->update($data);
        }else{
            $data = $post;
            $data['user_id'] = $company['user_id'];
            $data['company_id'] = $company['id'];
            $data['company_name'] = $company['company_name'];
            $data['company_mobile'] = $company['company_mobile'];
            $data['company_logo']   = $company['logo'];
            $data['status']  = 1;
            $data['ctime']   = time();
            $res  = db('position')->insertGetId($data);
        }
        
        if($res){   
            $this->ajaxReturn(null, '申请成功，等待审核', 200);
        }else{
            $this->ajaxReturn(null, '申请失败', 204);
        }
    }

    

    /**
     * 删除
     *
     * @return void
     */
    public function del()
    {
        $positionId = input('get.position_id');
        if(empty($positionId)){
            $this->ajaxReturn(null, '参数错误', 201);
        }

        $position = db('position')->find($positionId);
        if(empty($position)){
            $this->ajaxReturn(null, '无当前职位', 202);
        }

        $return = db('position')->delete($positionId);
        if($return !== false){
            $this->ajaxReturn(['id'=>$return], '取消成功', 200);
        }else{
            $this->ajaxReturn(null, '取消失败', 207);
        }
    }
    



    private function makeOrderNumber() {
        $order_sn = (intval(date('Y')) - 2010) . date('m') . date('d') . substr(time() , -5) . substr(microtime() , 2, 4);
        return str_pad($order_sn, 12, rand(0, 9));
    }
    


    /**
     * 充值
     *
     * @return void
     */
    public function pay()
    {
        $post = input('post.');
        $userId = $post['user_id'];

        if(empty($userId)){
            $this->ajaxReturn(null, '参数错误', 201);
        }

        $openid = db('user')->where('id', $userId)->value('openid');
        if(empty($openid)){
            $this->ajaxReturn(null, '无此用户', 204);
        }
        
        $data['order_no']   = $this->makeOrderNumber();
        $data['user_id']    = $post['user_id'];
        $data['company_id'] = $post['company_id'];
        $data['money']      = $post['money'];
        $data['status']     = 1;
        $data['ctime']      = time();
        $data['cdate']      = date('Y-m-d H:i:s');
        $res = db('order')->insertGetId($data);
        if($res){
            $weixinpay = new WeixinPay($openid, $data['order_no'], '会员充值', 'pay', $post['money'], '');
            $return = $weixinpay->pay();
            if(!empty($return['return_code']) && $return['return_code'] =='FAIL'){
                $this->ajaxReturn($return, $return['return_msg'], 204);
            }
            if($return['result_code'] == 'SUCCESS'){
                $this->ajaxReturn($return, '支付成功', 200);
            }else{
                $this->ajaxReturn($return, $return['err_code_des'], 204);
            }
        }else{
            $this->ajaxReturn(null, '支付是失败', 204);
        }
        
        
        
    }





}
